package com.authine.cloudpivot.ext.applicationservice;

import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.applicationservice.base.ApplicationBaseService;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.utils.BigDecimalUtils;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * 订单首页相关接口
 */
@Path("HomePageService")
@Slf4j
public class HomePageService extends ApplicationBaseService {

    @Path("orderData")
    @POST
    @Operation(summary = "首页-近期订单图表数据", tags = CUSTOM_SERVICE)
    public SingleResponse orderData() {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> legend = new HashMap<>();
        legend.put("data", new String[]{"订单数量", "订单金额"});
        result.put("legend", legend);

        String[] xData = new String[7];
        Long[] numData = new Long[7];
        BigDecimal[] amountData = new BigDecimal[7];
        SimpleDateFormat sf = new SimpleDateFormat(DateUtils.FORMAT_PATTERN_DATE);
        SimpleDateFormat sfMd = new SimpleDateFormat("MM-dd");
        Calendar calAfter = Calendar.getInstance();
        //最大数量
        Long maxNum = 0L;
        //最大金额
        BigDecimal maxAmount = BigDecimal.ZERO;
        for (int i = 6; i >= 0; i--) {
            if (i == 6) {
                calAfter.set(Calendar.DATE, calAfter.get(Calendar.DATE));
            } else {
                calAfter.set(Calendar.DATE, calAfter.get(Calendar.DATE) - 1);
            }
            Map<String, Object> ocNum = JdbcTemplateUtils.builder(ModelConst.T_ORDER_CUSTOMER).selectField("count(*) as num")
                    .eq("date(createdTime)", sf.format(calAfter.getTime())).queryForMap();
            Map<String, Object> ocAmount = JdbcTemplateUtils.builder(ModelConst.T_ORDER_CUSTOMER).selectField("sum(amount) as amount")
                    .eq("date(createdTime)", sf.format(calAfter.getTime())).queryForMap();
            xData[i] = sfMd.format(calAfter.getTime());
            numData[i] = (Long) ocNum.get("num");
            amountData[i] = BigDecimalUtils.round(BigDecimalUtils.div(ocAmount.get("amount"), new BigDecimal("10000")), 2);
            if (numData[i] > maxNum) {
                maxNum = numData[i];
            }
            if (amountData[i].compareTo(maxAmount) == 1) {
                maxAmount = amountData[i];
            }
        }
        //X轴数据构建
        List<Map<String, Object>> xAxis = new ArrayList<>();
        Map<String, Object> xAxi = new HashMap<>();
        xAxi.put("data", xData);
        xAxis.add(xAxi);
        result.put("xAxis", xAxis);
        //Y轴数据构建
        List<Map<String, Object>> yAxis = new ArrayList<>();
        Map<String, Object> leftAxi = new HashMap<>();
        leftAxi.put("type", "value");
        leftAxi.put("name", "数量(个)");
        leftAxi.put("min", 0);
        leftAxi.put("max", maxNum*1.2);
        leftAxi.put("axisLabel", new HashMap<>().put("formatter", "个"));
        yAxis.add(leftAxi);
        Map<String, Object> rightAxi = new HashMap<>();
        rightAxi.put("type", "value");
        rightAxi.put("name", "金额（万元）");
        rightAxi.put("min", 0);
        rightAxi.put("max", BigDecimalUtils.mul(maxAmount, new BigDecimal("1.2")));
        rightAxi.put("axisLabel", new HashMap<>().put("formatter", "万"));
        yAxis.add(rightAxi);
        result.put("yAxis", yAxis);
        //服务数据构建
        List<Map<String, Object>> series = new ArrayList<>();
        Map<String, Object> orderNum = new HashMap<>();
        orderNum.put("name", "订单数量");
        orderNum.put("data", numData);
        Map<String, Object> orderAmount = new HashMap<>();
        orderAmount.put("name", "订单金额");
        orderAmount.put("data", amountData);
        series.add(orderNum);
        series.add(orderAmount);

        result.put("series", series);
        return SingleResponse.of(result);
    }
}
